const mongo = require("../../../mongodb/mongoose");
const mark = 'mark'
const result = {
  msg: 'mark DAL [not run]',
  data: null,
}
module.exports = {
  // 查询记录点
  async getMarks(data) {
    let res = result
    let filter = {}
    filter.openId = data.openId
    filter.markTime = {$gte:data.sTime,$lte:data.eTime}
    if (data.lat) filter.latitude = data.lat
    if (data.lng) filter.longitude = data.lng
    filter.flag = {$gte: 1}
    let field = {}
    field.imgs = 0
    let mtSort = data.mtSort?data.mtSort:1
    let sort = {markTime: mtSort}
    let paging = {}
    paging.index = data.indexPage || 1
    paging.size = data.pageSize || 0
    // console.log(filter)
    let r = await mongo.find(mark,filter,field,sort,paging)
    // console.log(r)
    for (let i in r) {
      if (r[i].flag == 2) r[i].imgs = []
    }
    res.data = r
    res.msg = 'success'
    return res
  },
  // 按id查询记录点
  async getMarksById(data) {
    let res = result
    let filter = {}
    filter.openId = data.openId
    filter._id = {$in:data.recIds}
    filter.flag = {$gte: 1}
    let sort = {markTime: 1}
    let r = await mongo.find(mark,filter,{},sort,{})
    for (let i in r) {
      if (r[i].flag == 2) r[i].imgs = []
    }
    res.data = r
    res.msg = 'success'
    return res
  },
  // 创建记录点
  async createMark(data) {
    let res = result
    let d = {}
    d.openId = data.openId
    d.latitude = data.lat
    d.longitude = data.lng
    d.title = data.title
    d.address = data.address
    d.imgs = data.imgs
    d.markTime = data.markTime
    d.mem = data.mem
    let r = await mongo.insertMany(mark,d)
    if (r.writeErrors) {
      res.msg = r.writeErrors[0].err.errmsg 
    } else {
      if (r.errors) {
        if (r.errors.openId) res.msg = r.errors.openId.message
        if (r.errors.latitude) res.msg = r.errors.latitude.message
        if (r.errors.longitude) res.msg = r.errors.longitude.message
        if (r.errors.title) res.msg = r.errors.title.message
        if (r.errors.address) res.msg = r.errors.address.message
      } else {
        res.data = r
        res.msg = 'success'
      }
    }
    return res
  },
  // 删除记录点
  async delMark(data) {
    let res = result
    let filter = {}
    filter.openId = data.openId
    filter._id = data.recId
    let r = await mongo.deleteOne(mark,filter)
    if (r && r.deletedCount == 1) {
      res.data = [{recId: data.recId}]
      res.msg = 'success'
    }
    return res
  },
  // 删除记录点图片
  async delMarkImg(data) {
    let res = result
    let filter = {}
    filter.openId = data.openId
    filter._id = data.recId
    let uJson = {$set:{imgs:[]}}
    let r = await mongo.updateMany(mark,filter,uJson)
    if (r && r.modifiedCount == 1) {
      res.data = [{recId: data.recId}]
      res.msg = 'success'
    }
    return res
  },
  // 设置记录点flag
  async setMark(data) {
    let res = result
    let filter = {}
    filter.openId = data.openId
    filter._id = data.recId
    let uJson = {}
    if (data.flag == 'HIDE_MARK') uJson = {$set:{flag: 0}}
    if (data.flag == 'HIDE_IMG') uJson = {$set:{flag: 2}}
    if (data.flag == 'UPDATE_IMG') uJson = {$set:{imgs: data.imgs}}
    if (uJson == {}) {
      res.msg = '无效的设置参数'
    } else {
      let r = await mongo.updateMany(mark,filter,uJson)
      if (r && r.modifiedCount == 1) {
        res.data = [{recId: data.recId}]
        res.msg = 'success'
      }
    }
    return res
  },
  // 按openid查询所有记录日期
  async getMarksDate(data) {
    let res = result
    let oid = data.openId
    let r = await mongo.getMarksDateByOid(oid)
    res.data = r
    res.msg = 'success'
    return res
  },
}